package com.example.imtoken.modules.main.my.address.create

import android.content.Intent
import android.view.View
import com.blankj.utilcode.util.*
import com.example.imtoken.R
import com.example.imtoken.core.base.BaseActivity
import com.example.imtoken.core.utils.BusConfig
import com.example.imtoken.databinding.ActivityCreateAddressBinding
import com.example.imtoken.modules.main.my.address.create.select.SelectAddressTypeActivity
import com.example.imtoken.modules.scanning.ScanningActivity
import com.xuexiang.xui.widget.dialog.materialdialog.MaterialDialog

/**
 * 添加地址
 */
class CreateAddressActivity :
    BaseActivity<CreateAddressViewModel, ActivityCreateAddressBinding>(CreateAddressViewModel::class.java) {

    private val demoEthAddress = "0x076Ddcb7ABE50F76fcA5679f2AA1F2F24eEbaee0"

    private val edit = "edit"

    private val type by lazy {
        intent.getStringExtra("type")
    }

    private val id by lazy {
        intent.getIntExtra("id", 0)
    }

    private val coin by lazy {
        intent.getStringExtra("coin")
    }

    private val address by lazy {
        intent.getStringExtra("address")
    }

    override fun initLayoutId(): Int {
        return R.layout.activity_create_address
    }

    override fun init() {
        dataBinding.viewModel = viewModel
        initTitle()
        initView()
        initObserve()
        initClick()
        if (type == edit) {
            viewModel.findOneAddressBook(id)
        } else {
            viewModel.getCoinsList()
        }
    }

    private fun initClick() {
        dataBinding.llSelectType.setOnClickListener {
            hideInput()

            val map = HashMap<String, Any>()
            map["data"] = viewModel.getCoinsListData().value as Any
            map["p"] = viewModel.position
            BusUtils.postSticky(
                BusConfig.out_data_sticky_select_address_type_activity,
                map
            )
            val intent = Intent(this, SelectAddressTypeActivity::class.java)
            startActivityForResult(intent, 0)
        }
        dataBinding.headView.tvHeadFunction.setOnClickListener {
            when (viewModel.getCoins().value?.coin) {
                "eth" -> {
                    if (viewModel.addressLiveData.value?.length ?: 0 != demoEthAddress.length) {
                        ToastUtils.showShort(R.string.inputAddressError)
                        return@setOnClickListener
                    }
                }
            }

            if (type == edit) {
                if (viewModel.updateAddressBook(id)) {
                    setResult(200)
                    this.finish()
                }
            } else {
                if (viewModel.addAddressBook()) {
                    setResult(200)
                    this.finish()
                }
            }
        }
        ClickUtils.applyGlobalDebouncing(dataBinding.ivQRCode) {
            //扫描
            hideInput()
            val intent = Intent(this, ScanningActivity::class.java)
            intent.putExtra("type", "create_address")
            startActivityForResult(intent, 0)
        }
        dataBinding.tvDelete.setOnClickListener {
            MaterialDialog.Builder(this)
                .content(R.string.deleteTips)
                .title(R.string.tipsMsg)
                .positiveText(R.string.delete)
                .positiveColorRes(R.color.xui_config_color_red)
                .negativeText(R.string.cancel)
                .onPositive { dialog, _ ->
                    if (viewModel.deleteAddressBook(id)) {
                        dialog.dismiss()
                        setResult(200)
                        finish()
                    }
                }.onNegative { dialog, _ ->
                    dialog.dismiss()
                }
                .show()
        }
    }

    private fun initObserve() {
        viewModel.getCoins().observe(this) {
            dataBinding.item = it
        }
        viewModel.nameLiveData.observe(this) {
            viewModel._name = it
        }
        viewModel.addressLiveData.observe(this) {
            viewModel._address = it
        }
        viewModel.describeLiveData.observe(this) {
            viewModel._describe = it
        }
        viewModel.isSubmit.observe(this) {
            if (it) {
                dataBinding.headView.tvHeadFunction.setTextColor(ColorUtils.getColor(R.color.purple_700))
            } else {
                dataBinding.headView.tvHeadFunction.setTextColor(ColorUtils.getColor(R.color.manageWalletAddTextColor))
            }
            dataBinding.headView.tvHeadFunction.isClickable = it
        }
    }

    private fun initView() {
        if (type == edit) {
            dataBinding.tvDelete.visibility = View.VISIBLE
        } else {
            if (address != null) {
                address?.let { viewModel.setAddressValue(it) }
            } else {
                KeyboardUtils.showSoftInput(dataBinding.etAddress)
                dataBinding.tvDelete.visibility = View.GONE
            }
        }
    }

    private fun initTitle() {
        if (type == edit) {
            dataBinding.headView.tvHeadTitles.text = getString(R.string.editAddress)
        } else {
            dataBinding.headView.tvHeadTitles.text = getString(R.string.createAddress)
        }
        dataBinding.headView.vLine.visibility = View.INVISIBLE
        dataBinding.headView.ivHeadBack.setOnClickListener { this.finish() }
        dataBinding.headView.tvHeadFunction.visibility = View.VISIBLE
        dataBinding.headView.tvHeadFunction.text = getString(R.string.preservation)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        when (resultCode) {
            200 -> {
                val p = data?.getIntExtra("p", 0) ?: 0
                viewModel.selectCoins(p)
            }
            100 -> {
                //扫码返回地址
                val address = data?.getStringExtra("address")
                dataBinding.etAddress.setText(address)
                dataBinding.etAddress.setSelection(dataBinding.etAddress.text.length)
            }
        }
    }

}